package com.greenantmall.activity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.greenantmall.R;
import com.greenantmall.adapter.GoodsListAdapterForItem1;
import com.greenantmall.bean.ShoppingCart;
import com.greenantmall.bean.UIGoods;
import com.greenantmall.utils.Const;
import com.greenantmall.utils.UtilTools;
import com.lsarah.utils.SystemBarTintManager;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class GoodsListForIndependentStoreActivity extends BaseActivity implements OnItemClickListener
{
	public static final String KEY_GOODS_LIST = "key_goods_list";
	public static final String KEY_TITLE = "key_title";
	
	private View mBackView;
//	private View mTabContainer;
	private TextView mTvTitle;
//	private TextView mTvSalesVolume;
//	private TextView mTvPrice;
//	private TextView mTvNew;
	private GridView mGridView;
	private ProgressBar mProgressBar;
	private TextView mTvPrompt;
	
	private BaseAdapter mAdapter;
	private List<UIGoods> mDataList;
	private List<ShoppingCart> mShoppingCartList = new ArrayList<ShoppingCart>();
	
	private int mTotalCount = 0;
	private boolean isLoadingData = false;
	private int mCurrentOrderBy = 0;
	
	private OnClickListener mOnClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			if(v.equals(mBackView)) {
				GoodsListForIndependentStoreActivity.this.finish();
			}
		}
	};
	
	private BroadcastReceiver mReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			if(intent.getAction().equals(Const.SHOPPING_CART_CHANGED)) {
				refreshShoppingCartList();
			}
		}
	};
	
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        SystemBarTintManager.setContentView(this, R.layout.goods_list_for_independent_store, getResources().getColor(R.color.green));
        mBackView = findViewById(R.id.goods_list_top_arrow_left);
//        mTabContainer = findViewById(R.id.goods_list_tab_ll);
        mTvTitle = (TextView)findViewById(R.id.goods_list_top_des);
//        mTvSalesVolume = (TextView)findViewById(R.id.goods_list_tv_sales_volume);
//        mTvPrice = (TextView)findViewById(R.id.goods_list_tv_price);
//        mTvNew = (TextView)findViewById(R.id.goods_list_tv_new);
        mGridView = (GridView)findViewById(R.id.goods_list_gridview);
        mProgressBar = (ProgressBar)findViewById(R.id.goods_list_prgressbar);
        mTvPrompt = (TextView)findViewById(R.id.goods_list_tv_prompt);
        
        mBackView.setOnClickListener(mOnClickListener);
        mGridView.setOnItemClickListener(this);
        
        IntentFilter filter = new IntentFilter(Const.SHOPPING_CART_CHANGED);
        this.registerReceiver(mReceiver, filter);
        
        mDataList = new ArrayList<UIGoods>();
        retrieveData();
        refreshShoppingCartList();
    }
    
    @Override
    protected void onDestroy() {
    	super.onDestroy();
    	this.unregisterReceiver(mReceiver);
    }
    
    @SuppressWarnings("unchecked")
	private void retrieveData() {
    	// read title from intent
        String title = getIntent().getStringExtra(KEY_TITLE);
        if(!TextUtils.isEmpty(title)) {
        	mTvTitle.setText(title);
        }
        
        // read ArrayList<UIGoods> from intent
        ArrayList<UIGoods> tempList = null;
        Serializable serializableData = getIntent().getSerializableExtra(KEY_GOODS_LIST);
        if(serializableData != null) {
        	tempList = (ArrayList<UIGoods>)serializableData;
        }
        Log.d("CEXX", "CEXX ---> GoodsListActivity ---> tempList : " + (tempList==null ? tempList : tempList.size()));
        
    	mDataList.addAll(tempList);
    	mAdapter = new GoodsListAdapterForItem1(this, mDataList, mShoppingCartList);
        mGridView.setAdapter(mAdapter);
    }
    
    private void refreshShoppingCartList() {
		mShoppingCartList.clear();
		List<ShoppingCart> scList = UtilTools.getShoppingCartDatas();
		if(scList != null) {
			mShoppingCartList.addAll(scList);
		}
		
		if(mAdapter != null) {
			mAdapter.notifyDataSetChanged();
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Log.d("CEXX", "CEXX ---> GoodsListActivity ---> onItemClick ---> position : " + arg2);
//		UIGoods uiGoods = mDataList.get(arg2);
//		Intent intent = new Intent(this, DetailActivity.class);
//		intent.putExtra(DetailActivity.KEY_DETAIL_OBJECTID, uiGoods.getObjectId());
//		this.startActivity(intent); 
	}
}




